package cn.tuning.service.impl;

import cn.tuning.dao.OrderDao;
import cn.tuning.dao.UserDao;
import cn.tuning.entity.CustomerInfo;
import cn.tuning.entity.UserInfo;
import cn.tuning.pojo.APIResult;
import cn.tuning.service.UserService;
import cn.tuning.utils.IdWorkerUtil;
import cn.tuning.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lixiaolong
 * @date 2021-08-06 22:00
 * @description:
 */

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;
    @Autowired
    private OrderDao orderDao;
    /**
     * 登录
     * @param userInfo
     * @return
     */
    @Override
    public APIResult login(UserInfo userInfo) {
        APIResult apiResult = null;
        //使用username查询数据库
        UserInfo userMassage = this.userDao.getUserByUsername(userInfo.getUsername());
        //把传输进来的密码加密
        userInfo.setPassword(MD5Util.encode(userInfo.getPassword()));
        //判断该邮箱是否注册
        if (userMassage == null){
            apiResult = APIResult.errorUsername();
            return apiResult;
        }else if(!userInfo.getPassword().equals(userMassage.getPassword())){//比对密码
            apiResult = APIResult.errorPassword();
            return apiResult;
        }
        //邮箱密码正确
            apiResult = APIResult.success(userMassage);
        return apiResult;
    }

    /**
     * 注册
     * @param userInfo
     * @return
     */
    @Override
    public APIResult register(UserInfo userInfo) {
        APIResult apiResult = null;
        //使用username查询数据库
        UserInfo user = this.userDao.getUserByUsername(userInfo.getUsername());
        //判断该邮箱是否注册
        if (user != null) {
            apiResult = APIResult.registerFail();
            return apiResult;
        }
        //密码加密
        userInfo.setPassword(MD5Util.encode(userInfo.getPassword()));
        //生成uid插入
        IdWorkerUtil worker = new IdWorkerUtil(1,1,1);
        String uid = String.valueOf(worker.nextId());
        userInfo.setUid(uid);
        //插入用户信息
        this.userDao.insertUserInfo(userInfo);
        //插入用户表 邮箱+余额
        CustomerInfo customerInfo = new CustomerInfo(userInfo.getUsername(),"",0);
        this.userDao.insertCustomerInfo(customerInfo);
        //获取用户信息
        apiResult = APIResult.success(this.userDao.getUserByUsername(userInfo.getUsername()));
        return apiResult;
    }

    /**
     * 更新用户个人信息
     * @param userInfo
     * @return
     */
    @Override
    public APIResult updateUserInfo(UserInfo userInfo) {
        //把传输进来的密码加密
        userInfo.setPassword(MD5Util.encode(userInfo.getPassword()));
        this.userDao.updateUserInfo(userInfo);
        APIResult apiResult = APIResult.success();
        return apiResult;
    }


    /**
     * 注册时发送验证码
     * @param username 邮箱
     * @return
     */
    @Override
    public APIResult verify(String username) {
        APIResult apiResult = null;
        //使用username查询数据库
        UserInfo user = this.userDao.getUserByUsername(username);
        //判断该邮箱是否注册 是空说明没有注册,说明可以注册
        if (user != null) {
            apiResult = APIResult.registerFail(); //邮箱已存在 : 401
            return apiResult;
        }
        //返回成功
        apiResult = APIResult.success();
        return apiResult;
    }

}
